const { Controller } = require('dx-router-admin');
const dxEpay = require('dx-epay');
module.exports = class Order extends Controller {
	constructor(ctx) {
		super(ctx);
	}
	// 下单
	async create({ vip_id, price, day }) {
		if (!vip_id) {
			return this.$dx.fail('参数错误');
		}
		const info = await this.$dx.service.vip.where('_id', vip_id).find();
		if (!info || !info.price_list.length) {
			return this.$dx.fail('VIP不存在');
		}
		let row = info.price_list.find((v) => {
			return v.day == day && v.price == price;
		});
		if (!row) {
			return this.$dx.fail('VIP价格不存在');
		}
		const order = await this.$dx.service.order
			.where('vip_id', vip_id)
			.where('amount', Number(row.price))
			.where('status', 0)
			.where('user_id', this.$dx.auth.uid)
			.find();
		if (order) {
			await this.$dx.service.order.save({
				_id: order._id,
				vip_price: row,
				name: info.name,
			});
			// 订单复用
			return this.$dx.success('订单已存在', {
				order_sn: order.order_sn,
			});
		}
		const order_sn = this.$dx.service.order.getOrderSn();
		// 创建订单
		const orderData = {
			order_sn,
			vip_id: vip_id,
			vip_price: row,
			user_id: this.$dx.auth.uid,
			amount: row.price,
			title: info.name,
			level:info.level,
			ip: this.$dx.request.ip,
			status: 0,
		};
		const orderId = await this.$dx.service.order.insert(orderData);
		if (!orderId) {
			return this.$dx.fail('订单创建失败');
		}
		return this.$dx.success('订单创建成功', {
			order_sn,
		});
	}
	async pay({order_sn, paytype, method}) {
		if (!['wechat', 'alipay'].includes(paytype)) {
			return this.$dx.fail('支付类型错误');
		}
		if (!['mp', 'miniapp', 'app', 'scan', 'wap'].includes(method)) {
			return this.$dx.fail('支付方式错误');
		}
		// 获取订单信息
		const order = await this.$dx.service.order.where('order_sn', order_sn).find();
		if (!order) {
			return this.$dx.fail('订单不存在');
		}
		if (order.status === 1) {
			return this.$dx.fail('订单已支付');
		}
		if (order.status === 2) {
			return this.$dx.fail('订单已失效');
		}
		if (order.user_id !== this.$dx.auth.uid) {
			return this.$dx.fail('订单不属于当前用户');
		}
		// 微信支付更换订单号【不同端订单号不同重复，会存在旧订单利用】
		// if (order.paytype === 'wechat' && order.method !== method) {
		// 	order_sn = this.getOrderSn();
		// 	await this.$dx.service.order.where('_id', order._id).update({ order_sn });
		// }
		const payData = {
			outTradeNo: order_sn,
			body: order.title,
			totalFee: order.amount * 100,
			spbillCreateIp: this.$dx.request.ip,
			notifyUrl: '/api/callback/notify/paytype/' + paytype + '/method/' + method,
		};
		if (method === 'mp' || method === 'miniapp') {
			// 【微信/支付宝】小程序支付,公众号支付
			const user = await this.$dx.getUserInfo(this.$dx.auth.uid);
			if (paytype === 'alipay') {
				if (!user.ali_openid) {
					return this.$dx.fail('支付宝用户信息不存在');
				}
				payData.openid = user.ali_openid;
			} else {
				const key = method === 'miniapp' ? 'mp' : 'h5';
				if (!user.wx_openid || !user.wx_openid[key]) {
					return this.$dx.fail('微信用户信息不存在');
				}
				payData.openid = user.wx_openid[key];
			}
		}
		// 创建订单
		const pay = new dxEpay(paytype, method, 'vip');
		const result = await pay.createOrder(payData);
		// 更新订单
		await this.$dx.service.order.save({
			_id: order._id,
			paytype,
			method,
		});
		return this.$dx.success('创建支付单成功', result);
	}
};
